//
//  FPPrivacyController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/14.
//

#import "FPPrivacyController.h"

@interface FPPrivacyController () <TTTAttributedLabelDelegate>
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (weak, nonatomic) IBOutlet UILabel *titleLab;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *windowHeight;

//@property (weak, nonatomic) IBOutlet FPCustomTextView *textView1;
@property (weak, nonatomic) IBOutlet TTTAttributedLabel *contentLab;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;

@property (nonatomic , strong) NSMutableArray *protocolSource;

@end

@implementation FPPrivacyController

LazyLoadDataSource(protocolSource);

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWiFi || status == AFNetworkReachabilityStatusReachableViaWWAN) {
            NSLog(@"网络恢复，立即发起请求");
            [self getProtocol];
        }
    }];
    
    // 开始监听
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    
    // 进入 App 时先检查当前网络状态
    if ([AFNetworkReachabilityManager sharedManager].reachable) {
        [self getProtocol];
    }
    
    [self initUI];
}

- (void)initUI {
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.backView.clipsToBounds = YES;
    self.backView.layer.cornerRadius = 8.f;
    
    self.contentView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:.6f];
    
    self.btn1.clipsToBounds = YES;
    self.btn1.layer.cornerRadius = 22.f;
    self.btn1.backgroundColor = UIColor.whiteColor;
    [self.btn1 setTitleColor:MAIN_BLUE_COLOR forState:(UIControlStateNormal)];
    self.btn1.layer.borderColor = MAIN_BLUE_COLOR.CGColor;
    self.btn1.layer.borderWidth = 1.f;
    
    self.btn2.clipsToBounds = YES;
    self.btn2.layer.cornerRadius = 22.f;
    self.btn2.backgroundColor = MAIN_BLUE_COLOR;
    [self.btn2 setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    
//    self.textView1.userInteractionEnabled = NO;
    
//    self.textView1.textColor = [UIColor hexColor:@"#3A3A3A"];
//    self.privacyLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.contentLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.titleLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    self.titleLab.textColor = [UIColor hexColor:@"#3A3A3A"];
    
    self.titleLab.backgroundColor = UIColor.clearColor;
//    self.textView1.backgroundColor = UIColor.clearColor;
    self.contentLab.backgroundColor = UIColor.clearColor;
//    self.privacyLab.backgroundColor = UIColor.clearColor;
    
    self.titleLab.text = @"同意政策";
    
    NSString *text1 = @"为了向您提供借款服务，我们会在您同意并授权的基础上收集、使用您借款必要的个人信息，如身份信息、银行卡信息、位置信息、设备信息等，用于完成交易、授权评估、保障安全等服务。我们将在首次调用时逐项询问您是否允许使用该权限。您可以在我们询问时开启相关权限，也可以在设备系统“设置”里管理相关权限:\n1.相机权限:在向您提供客服、人脸等图像识别服务时，您可进行拍摄和图片视频处理\n2.相册权限:在向您提供客服、图片上传服务时，您可进行图片处理\n3.通知权限:您可以及时了解审核结果、借款进度\n4.网络权限:向您提供借款产品和服务\n5.存储权限:需要使用您的本地缓存，使贷款流程更顺畅";
    NSString *text2 = @"有关个人信息收集、使用更详细的约定请阅读《众鑫花隐私政策》和《众鑫花注册协议》全文。我们承诺会不断完善安全技术和制度措施，确保您个人信息的安全。";
    
    NSString *finalStr = [NSString stringWithFormat:@"%@\n\n%@", text1, text2];
    
    CGFloat height = [UIView heightWithString:finalStr labelWidth:SCREEN_WIDTH - 48.f - 72.f fontSize:14.f];
    
    self.windowHeight.constant = 350.f + height - 67.f - 139.f;
//    [self getProtocol];
    
    self.contentLab.text = finalStr;
}

#pragma mark - 获取协议
- (void)getProtocol {
    
    NSString *text1 = @"为了向您提供借款服务，我们会在您同意并授权的基础上收集、使用您借款必要的个人信息，如身份信息、银行卡信息、位置信息、设备信息等，用于完成交易、授权评估、保障安全等服务。我们将在首次调用时逐项询问您是否允许使用该权限。您可以在我们询问时开启相关权限，也可以在设备系统“设置”里管理相关权限:\n1.相机权限:在向您提供客服、人脸等图像识别服务时，您可进行拍摄和图片视频处理\n2.相册权限:在向您提供客服、图片上传服务时，您可进行图片处理\n3.通知权限:您可以及时了解审核结果、借款进度\n4.网络权限:向您提供借款产品和服务\n5.存储权限:需要使用您的本地缓存，使贷款流程更顺畅";
    
    [FPProtocolModel getProtocolWithType:(FPProtocolTypeFirstEntry) success:^(NSInteger code, NSString * _Nonnull message, NSArray *list) {
        [MBManager hideLoading];
        
        [self.protocolSource removeAllObjects];
        if (code == 200) {
            
            for (FPProtocolModel *model in list) {
                
                [self.protocolSource addObject:model];
            }
            
            NSMutableString *protocolStr = NSMutableString.string;
            
            for (NSInteger i = 0; i < self.protocolSource.count; i ++) {
                
                FPProtocolModel *model = self.protocolSource[i];
                
                if (i == self.protocolSource.count - 1) {
                    
                    [protocolStr appendFormat:@"%@",model.agreementName];
                }
                else {
                    [protocolStr appendFormat:@"%@和",model.agreementName];
                }
            }
            
            NSString *text = [NSString stringWithFormat:@"%@\n\n有关个人信息收集、使用更详细的约定请阅读%@全文。我们承诺会不断完善安全技术和制度措施，确保您个人信息的安全。", text1, protocolStr];
            
            NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
            
            [NSString stringToAttribute:attStr font:FONT(14.f) range:NSMakeRange(0, text.length)];
            
            for (NSInteger i = 0; i < self.protocolSource.count; i ++) {
                
                FPProtocolModel *model = self.protocolSource[i];
                
                NSRange range = [text rangeOfString:[NSString stringWithFormat:@"%@", model.agreementName]];
                
                [NSString stringToAttribute:attStr color:MAIN_BLUE_COLOR range:range];
                
                [self.contentLab addLinkToURL:[NSURL URLWithString:model.address] withRange:range];
            }
            
            CGFloat height = [UIView heightWithString:text labelWidth:SCREEN_WIDTH - 48.f - 72.f fontSize:14.f];
            self.windowHeight.constant = 350.f + height - 67.f - 139.f;
            
            
            self.contentLab.attributedText = attStr;
            self.contentLab.delegate = self;
        }
        
    } failure:^(NSError * _Nonnull error) {
        [MBManager hideLoading];
    }];
}

#pragma mark - TTTAttributedLabelDelegate 处理协议点击
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
 
//    [MBManager showText:url.absoluteString];
    for (FPProtocolModel *model in self.protocolSource) {
        
        if ([url.absoluteString isEqualToString:model.address]) {
            
            FPMainWebController *vc = [[FPMainWebController alloc] init];
            
            vc.model = model;
            vc.protocolType = FPProtocolTypeLogin;
            
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
    }
}

- (IBAction)dissagreeAction:(UIButton *)sender {
    
    exit(0);
}

- (IBAction)agreeAction:(UIButton *)sender {
    
    [self signProtocolWithParams:self.protocolSource];
    
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:FIRST_LOGIN_FILE_PATH];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:path] ?: [NSMutableDictionary dictionary];

    dict[PRIVACY_CONTROLLER] = PRIVACY_CONTROLLER;
    [dict writeToFile:path atomically:YES];
    
    [UIApplication sharedApplication].delegate.window.rootViewController = [[FPMainTabBarController alloc] initWithContext:nil sourcetype:@"userInfo.platformType"];
//    [UIApplication sharedApplication].delegate.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[FPLoginController new]];
}


@end
